// pages/user/user.js

var http = require("../../utils/http.js");
var util = require("../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    orderAmount: '',
    sts: '',
    collectionCount: 0,
    canDistribution: false, //分销开关
    isAuthInfo: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userInfo = {}
    userInfo.nickName = wx.getStorageSync('loginResult').nickName
    userInfo.pic = wx.getStorageSync('loginResult').pic
    this.setData({
      userInfo: userInfo
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 查看是否授权
    util.checkAuthInfo(() => {
      //加载订单数字
      wx.showLoading();
      this.getOrderCount()
    }, true)
    this.setData({
      isAuthInfo: Boolean(wx.getStorageSync('loginResult').userId)
    })
    this.getDistInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    this.setData({
      orderAmount: {}
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 获取订单数字
   */
  getOrderCount() {
    var params = {
      url: "/p/user/centerInfo",
      method: "GET",
      dontTrunLogin: true,
      data: {},
      callBack: (res) => {
        wx.hideLoading();
        this.setData({
          orderAmount: res.orderCountData
        });
      }
    };
    http.request(params);
  },
  /**
   * 查询分销相关信息
   */
  getDistInfo() {
    //查询分销开关是否开启
    http.request({
      url: "/p/distribution/distributionBasicSet/canDistribution",
      method: "GET",
      dontTrunLogin: true,
      data: {
        shopId: 1,
      },
      callBack: (res) => {
        this.setData({
          canDistribution: res == 1
        });
      }
    })
  },


  /**
   * 跳转会员中心
   */
  toMemberIndex() {
    util.checkAuthInfo(() => {
      wx.navigateTo({
        url: '/packageMemberIntegral/pages/memberIndex/memberIndex',
      })
    })
  },

  /**
   * 跳转分销员中心
   */
  toDistCenter: function () {
    //判断是否为分销员
    var isDister = wx.getStorageSync("isDister");
    util.checkAuthInfo(() => {
      http.request({
        url: "/p/distribution/user/distributionUserInfo",
        method: "GET",
        data: {
          shopId: 1,
        },
        callBack: (res) => {
          console.log(res)
          if (res && res.state == 1) {
            wx.setStorageSync("isDister", true);
            wx.setStorageSync("distCardNo", res.cardNo);
            wx.setStorageSync('distInfo', res)
            wx.navigateTo({
              url: '/pages/dis-center/dis-center',
            })
          } else if (res && res.state == 0) {
            wx.showToast({
              title: '您提交的申请正在审核中',
              icon: "none"
            })
          } else if (res && res.state == 3) {
            wx.showModal({
              title: '',
              content: '您的申请未通过，具体原因请咨询客服，重新进行申请？',
              confirmColor: "#e43130",
              success(res2) {
                if (res2.confirm) {
                  wx.navigateTo({
                    url: '/pages/applyDist/applyDist',
                  })
                } else if (res2.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            wx.navigateTo({
              url: '/pages/applyDist/applyDist',
            })
          }
        }
      })
    })
  },


  toAfterSalesPage: function () {
    util.checkAuthInfo(() => {
      wx.navigateTo({
        url: '/pages/afterSales/afterSales',
      })
    })
  },

  toCouponCenter: function () {
    util.checkAuthInfo(() => {
      wx.navigateTo({
        url: '/pages/couponCenter/couponCenter',
      })
    })
  },

  toMyCouponPage: function () {
    util.checkAuthInfo(() => {
      wx.navigateTo({
        url: '/pages/myCoupon/myCoupon',
      })
    })
  },

  toAddressList: function () {
    util.checkAuthInfo(() => {
      wx.navigateTo({
        url: '/pages/delivery-address/delivery-address',
      })
    })
  },

  // 跳转绑定手机号
  toBindingPhone: function () {
    wx.navigateTo({
      url: '/pages/binding-phone/binding-phone',
    })
  },

  toOrderListPage: function (e) {
    var sts = e.currentTarget.dataset.sts;
    util.checkAuthInfo(() => {
      wx.navigateTo({
        url: '/pages/orderList/orderList?sts=' + sts,
      })
    })

  },

  /**
   * 查询所有的收藏量
   */
  showCollectionCount: function () {
    var ths = this;
    wx.showLoading();
    var params = {
      url: "/p/user/collection/count",
      method: "GET",
      data: {},
      callBack: function (res) {
        wx.hideLoading();
        ths.setData({
          collectionCount: res
        });
      }
    };
    http.request(params);
  },

  /**
   * 我的收藏跳转
   */
  myCollectionHandle: function () {
    util.checkAuthInfo(() => {
      var url = '/pages/prod-classify/prod-classify?sts=5';
      var id = 0;
      var title = "我的收藏商品";
      if (id) {
        url += "&tagid=" + id + "&title=" + title;
      }
      wx.navigateTo({
        url: url
      })
    })
  },

  /**
   * 退出登录
   */
  logout: function () {
    wx.clearStorage()
    wx.removeTabBarBadge({
      index: 2,
    })
    wx.navigateTo({
      url: "/pages/login/login"
    })
  },

  /**
   *  跳转登录页面
   */
  toLogin() {
    wx.navigateTo({
      url: '/pages/login/login',
    })
  }
})